﻿using Common;
using DataLibrary;
using System;
using System.Linq;
using System.Windows.Forms;

namespace TicketSystem
{
    public partial class Form_OperatorAdd : Form
    {
        public Form_OperatorAdd()
        {
            InitializeComponent();
        }
        private TicketSystemEntities tse = new TicketSystemEntities();
        private PublicMethod pm = new PublicMethod();
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (pm.IsTelephone(this.txt_Phone.Text))
            {
                Operators ope = new Operators();
                ope.RoleID = Convert.ToInt32(this.com_Role.SelectedValue);
                ope.Name = this.txt_Name.Text;
                ope.IDCard = this.txt_IdCard.Text;
                ope.Phone = this.txt_Phone.Text;
                ope.Address = this.txt_Address.Text;
                ope.Password = pm.EncryptString("123456");
                ope.Created_at = System.DateTime.Now;
                ope.Updated_at = System.DateTime.Now;
                tse.Operators.Add(ope);
                if (tse.SaveChanges() >= 1)
                {
                    MessageBox.Show("添加成功");
                    this.Close();
                    //Operator_Manage om = new Operator_Manage();
                    //om.Show();
                }
                else
                {
                    MessageBox.Show("添加失败了！");
                }
            }
            else
            {
                MessageBox.Show("手机号输入有误");
            }
        }

        private void Form_OperatorAdd_Load(object sender, EventArgs e)
        {
            var role = tse.Roles.Where(u => true).Select(u => new { u.RoleID, u.RoleName }).ToList();
            this.com_Role.DataSource = role;
            this.com_Role.ValueMember = "RoleID";
            this.com_Role.DisplayMember = "RoleName";
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (pm.IsTelephone(this.txt_Phone.Text))
            {
            Operators ope = new Operators();
            ope.RoleID = Convert.ToInt32(this.com_Role.SelectedValue);
            ope.Name = this.txt_Name.Text;
            ope.IDCard = this.txt_IdCard.Text;
            ope.Phone = this.txt_Phone.Text;
            ope.Address = this.txt_Address.Text;
            ope.Password = pm.EncryptString("123456");
            ope.Created_at = System.DateTime.Now;
            ope.Updated_at = System.DateTime.Now;
            tse.Operators.Add(ope);
            if (tse.SaveChanges() >= 1)
            {
                MessageBox.Show("添加成功");
                this.txt_Name.Text = null;
                this.txt_IdCard.Text = null;
                this.txt_Address.Text = null;
                this.txt_Phone.Text = null;
            }
            else
            {
                MessageBox.Show("添加失败了！");
            }
            }
            else
            {
                MessageBox.Show("手机号输入有误");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
